package cn.myzqu.sign.common.message.sms;

import cn.myzqu.sign.common.message.common.SendResult;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;

import java.io.IOException;
import java.util.ArrayList;

/**
 * @Author: 的川
 * @Date: 2018/12/2.
 */
public  class SmsTemplate {

    /**
     * 短信应用SDK AppID
     */
    private  int APPID ;
    /**
     * 短信应用SDK AppKey
     */
    private  String APPKEY ;
    /**
     * 签名内容
     */
    private  String SMSSIGN ;

    /**
     * 短信模板ID
     */
    private int templateId ;
    /**
     * 短信模板
     */
    private String template ;


    public SmsTemplate() {
    }

    public SmsTemplate(int APPID, String APPKEY, String SMSSIGN) {
        this.APPID = APPID;
        this.APPKEY = APPKEY;
        this.SMSSIGN = SMSSIGN;
    }

    public static void main(String[] args){
        String phone = "17876253486";
        SmsTemplate smsTemplate = new SmsTemplate();
        ArrayList params = new ArrayList();
        params.add("肇图周末志愿活动(周六早上)");
        params.add("陆泽宇");
        params.add("13556573036");
        SendResult result = smsTemplate.sendEnrollSuccess(phone,params);
        System.out.println(result);

    }

    public int getTemplateId() {
        return templateId;
    }

    public void setTemplateId(int templateId) {
        this.templateId = templateId;
    }

    public String getTemplate() {
        return template;
    }

    public void setTemplate(String template) {
        this.template = template;
    }

    /**
     * 获取发送的消息内容
     * @return
     */
    private String getContent(ArrayList params){
        //获取模本
        String content = "【"+ this.SMSSIGN + "】"+ this.template;
        for(int i = 1;i<=params.size();i++){
            //将{1}替换为实际参数
            content = content.replace("{"+i+"}",params.get(i-1).toString());
        }
        return content;
    }

    /**
     * 发送信息
     * @return
     */
    public SendResult send(String phone , ArrayList params ){
        SmsSingleSender singleSender = new SmsSingleSender(this.APPID, this.APPKEY);
        SmsSingleSenderResult smsSingleSenderResult ;
        SendResult sendResult = new SendResult();
        //发送内容
        sendResult.setContent(getContent(params));
        try {
            smsSingleSenderResult = singleSender.sendWithParam("86", phone,
                    this.templateId, params, this.SMSSIGN, "", "");
            // 发送结果：{"result":0,"errmsg":"OK","ext":"","sid":"18:6c4d0355cd3d44d9b028303a78a73824","fee":1}
            sendResult.setCode(smsSingleSenderResult.result+"");
            sendResult.setMsg(smsSingleSenderResult.errMsg);
            return sendResult;
        } catch (HTTPException e) {
            sendResult.setCode(e.getStatusCode()+"");
            sendResult.setMsg(e.getMessage());
        } catch (IOException e) {
            sendResult.setMsg(e.getMessage());
        }
        return sendResult;
    }

    /**
     * 发送报名成功信息
     * @param phone
     * @param params
     * @return
     */
    public SendResult sendEnrollSuccess(String phone , ArrayList params){
        this.templateId = 226779;
        this.template = "您已成功报名活动{1}，请进入小程序活动页面，点击群聊按钮加入活动群聊，若有疑问请联系活动负责人{2}-{3}。";
        return send(phone , params);
    }







}
